package com.detrum.sportcamera.nfstask;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.detrum.NFSVideo;
import com.detrum.bean.FileBean;
import com.detrum.sqlite.SqliteMediaOperation;
import com.detrum.utils.LogUtils;

import java.io.File;

/**
 * Created by abc on 2018/2/10.
 */

public class NfsDeleteListTask extends AsyncTask<FileBean,String,Void> {
    private Context context;
    private int index;
    private NfsDeleteListCall nfsDeleteListCall;
    private boolean isStop;

    public NfsDeleteListTask(Context context, int index, NfsDeleteListCall nfsDeleteListCall) {
        this.context = context;
        this.index = index;
        this.nfsDeleteListCall = nfsDeleteListCall;
    }

    public interface NfsDeleteListCall{
        void itemComplete(String fileName);
        void itemFail(String fileName);
        void finishAll();
    }

    public void Stop(){
        isStop = true;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        nfsDeleteListCall.finishAll();
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        String[]strs=values[0].split("----");
        if(strs[1].equals("fail")){
            nfsDeleteListCall.itemFail(strs[0]);
        }else if(strs[1].equals("success")){
            nfsDeleteListCall.itemComplete(strs[0]);
        }
    }

    @Override
    protected Void doInBackground(FileBean... fileBeans) {
        if(fileBeans!=null&&fileBeans.length!=0){
            int length = fileBeans.length;
            LogUtils.e("length = "+length);
            for (int i = 0; i < length; i++) {
                LogUtils.e("getNfsPath = "+fileBeans[i].getNfsPath());
                if(isStop){
                    break;
                }
                int unlink = NFSVideo.unlink(fileBeans[i].getNfsPath(),index);
                LogUtils.e("unlink = "+unlink);
                if(unlink<0){
                    publishProgress(fileBeans[i].getFileName()+"----fail");
                }else{
                    String fileName = fileBeans[i].getFileName();
                    int[]flags = SqliteMediaOperation.getInstence(context).getFlags(fileName);
                    LogUtils.i("flag = "+flags[0]);
                    if(flags[0]==0||flags[0]==10){
                        String cacheName = SqliteMediaOperation.getInstence(context).getFilePath(fileName);
                        String thumbPath = SqliteMediaOperation.getInstence(context).getThumbPath(fileName);
                        if(cacheName!=null&&cacheName.length()!=0){
                            File file = new File(cacheName);
                            if(file.exists()){
                                file.delete();
                            }
                        }
                        if(thumbPath!=null&&thumbPath.length()!=0){
                            File file1 = new File(thumbPath);
                            if(file1.exists()){
                                file1.delete();
                            }
                        }
                        SqliteMediaOperation.getInstence(context).DeleteItem(fileName);
                    }else if(flags[0]==1){
                        SqliteMediaOperation.getInstence(context).UpdataItemIsNfs(fileName,0);
                    }
                    publishProgress(fileBeans[i].getFileName()+"----success");
                }
                NFSVideo.close(index);
            }
        }
        return null;
    }
}
